﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data.SqlClient;
using System.Data;
using DAL.Entities;

namespace DAL.EntitiesDAL
{
     public class savingDAL: BaseDAL, UTL.IBaseDAL
    {
        public int Count()
        {
            throw new NotImplementedException();
        }

        public DataTable Select()
        {
            throw new NotImplementedException();
        }

        public DataTable Select(object obj)
        {
            throw new NotImplementedException();
        }

        public object GetByKey(object key)
        {
            throw new NotImplementedException();
        }

        public bool Delete(string id)
        {
            throw new NotImplementedException();
        }

        public bool Insert(object obj)
        {
            var o = (saving)obj;
            string sql = @"set dateformat dmy; INSERT INTO SAVING(MAPHIEU, NGAYTAOPHIEU, MABAN, TINHTIEN) VALUES('{0}',N'{1}', N'{2}', '{3}') ";
            sql = string.Format(sql, o.MAPHIEU, o.NGAYTAOPHIEU, o.MABAN, o.TINHTIEN);
            return ExecuteNonQuery(sql) > 0 ? true : false;
        }

        public bool Update(object obj)
        {
            throw new NotImplementedException();
        }

        public DataTable Search(string name)
        {
            throw new NotImplementedException();
        }

        public string tong_thu_ht(string ten)
        {
            var sql = @"
                            set dateformat dmy; 
                            SELECT sum(tinhtien) as tong
                            FROM saving
                            WHERE  day(ngaytaophieu) = day('{0}') 
                            and month(ngaytaophieu) = month('{0}') 
                            and year(ngaytaophieu) = year('{0}')
                            ";
            sql = string.Format(sql, ten);
            DataTable dt = ExecuteQuery(sql);
            return dt.Rows.Count > 0 ? dt.Rows[0]["tong"] + " " : "0";
        }

        public string loinhuan_thang(string ten)
        {
            var sql = @"
                            set dateformat dmy; 
                            SELECT sum(tinhtien) as tong
                            FROM saving
                            WHERE   month(ngaytaophieu) = month('{0}') 
                            and year(ngaytaophieu) = year('{0}')
                            ";
            sql = string.Format(sql, ten);
            DataTable dt = ExecuteQuery(sql);
            return dt.Rows.Count > 0 ? dt.Rows[0]["tong"] + " " : "0";
        }

        public string loinhuan_nam(string ten)
        {
            var sql = @"
                            set dateformat dmy; 
                            SELECT sum(tinhtien) as tong
                            FROM saving
                            WHERE year(ngaytaophieu) = year('{0}')
                            ";
            sql = string.Format(sql, ten);
            DataTable dt = ExecuteQuery(sql);
            return dt.Rows.Count > 0 ? dt.Rows[0]["tong"] + " " : "0";
        }
    }
}
